package com.cmb;

import com.cryptape.cita.crypto.sm2.SM2;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

/**
 * @author CMB
 */
public class Test {

    public static void main(String args[]) throws Exception {

        // --------------------- SM2生成国密算法公私钥对测试 -------------------
        KeyInfo keyInfo = SM2Algo.newKeyPair();
        // 生成的公钥自行妥善保存
        System.out.println("SM2私钥：" + keyInfo.privateKey);
        // 生成的私钥自行妥善保存，不应向任何第三方展示
        System.out.println("SM2公钥：" + keyInfo.publicKey);
        // 生成的地址提供给招商银行，在开放许可链管理平台准入
        System.out.println("SM2地址：" + keyInfo.address);

        System.out.println("****************************************");

        // --------------------- SM2非对称加解密测试 -------------------
        String data = "ASDF1234我爱北京天安门”%……&*";
        byte[] sm2CipherText = SM2Algo.encrypt(data);
        System.out.println("SM2加密：" + ByteUtils.toHexString(sm2CipherText));
        String sm2PlainText = SM2Algo.decrypt(sm2CipherText);
        System.out.println("SM2解密：" + sm2PlainText);

        System.out.println("****************************************");

        // --------------------- SM2签名验签测试 -------------------
        SM2.Signature sign = SM2Algo.sign(data);
        System.out.println("SM2签名：" + sign.getSign());
        boolean result = SM2Algo.verify(data, sign);
        System.out.println("SM2验签：" + result);

        System.out.println("****************************************");

        // --------------------- SM3计算合同文件HASH值测试 -------------------
        // f66f821c5c703105d4a593ac75d84b704804514f6b20260dcc0730b14eca7064
        String hash1 = SM3Algo.hash("E:\\Projects\\family-trust-gm\\测试文件1.pdf");
        // 906c2ee53587d2f26065a4b1ca3a2f86aa95232babb6ed1fc7eefd54d0bb30cb
        String hash2 = SM3Algo.hash("E:\\Projects\\family-trust-gm\\测试文件2.txt");
        // 0806e48f920457aa009900b6222d9324220d173d6e5ceca2606e0c975c9ce6d3
        String hash3 = SM3Algo.hash("E:\\Projects\\family-trust-gm\\测试文件3.jpg");

        // 以下三个hash请请根据测试标准结果校验
        System.out.println("SM3哈希：" + hash1);
        System.out.println("SM3哈希：" + hash2);
        System.out.println("SM3哈希：" + hash3);
        // 55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23
        System.out.println("SM3哈希：" + SM3Algo.hashOfPlainText("test"));

        System.out.println("****************************************");

        // --------------------- SM4对称加解密测试 -------------------
        byte[] sm4Key = SM4Algo.generateKey(128);
        System.out.println("SM4密钥：" + ByteUtils.toHexString(sm4Key));
        byte[] sm4CipherText = SM4Algo.encryptEcbPadding(sm4Key, data.getBytes());
        System.out.println("SM4加密：" + ByteUtils.toHexString(sm4CipherText));
        byte[] sm4PlainText = SM4Algo.decryptEcbPadding(sm4Key, sm4CipherText);
        System.out.println("SM4解密：" + new String(sm4PlainText));
    }
}
